#include <iostream>
#include <QDebug>

#include "gameworld.h"
#include "worldcommand.h"
#include "watertile.h"

using namespace std;

bool CreateShipCommand::execute()
{

    ShipObject obj = GameWorld::instance().createShip(type, y, x, image, portrait);
//    if (obj != NULL) {
        //obj->setImage(image);
        GameWorld::instance().addShip(obj);

        cout << "created ship id " << obj.getId() << endl;
        return true;
//    } else {
//        cout << "create failed: unknown type " << type << endl;
//        return false;
//    }
}

bool CreateTileCommand::execute()
{

    WaterTile *obj = GameWorld::instance().createTile(type, row, col, image);
    if (obj != NULL) {
        obj->setSelected(false);
        obj->setHit(false);
        obj->setTypeShip("none");
        GameWorld::instance().addTile(obj);


        cout << "created tile image " << obj->getImage() << endl;
        return true;
    } else {
        cout << "create tile failed: " << endl;
        return false;
    }
}

bool CreateOpponentTileCommand::execute()
{
    WaterTile *obj = GameWorld::instance().createOpponentTile(type, row, col, image);
    if (obj != NULL) {
        obj->setSelected(false);
        obj->setHit(false);
        obj->setTypeShip("none");
        GameWorld::instance().addOpponentTile(obj);


        cout << "created tile image " << obj->getImage() << endl;
        return true;
    } else {
        cout << "create tile failed: " << endl;
        return false;
    }

}

bool MoveCommand::execute()
{
    ShipObject obj = GameWorld::instance().getById(id);
//    if (obj != NULL) {
        obj.setRow(row);
        obj.setCol(col);

        cout << "moved ship to " << col << "," << row << endl;
        return true;
//    } else {
//        cout << "move failed: no object " << id << endl;
//        return false;
//    }
}

void WorldBuilder::doCommand(WorldCommand *cmd)
{
    cmd->execute();
}
